﻿using CommonManagement.Applcation.Command;
using ERPPublic;
using ERPPublic.eneity;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PersonnelManagement.Applcation.Command.ProvinceCommand;
using PersonnelManagement.Application.Command;
using PersonnelManagement.Application.Command.Clientcategory;
using PersonnelManagement.Application.Command.ClientCommand;
using PersonnelManagement.Domain.Dto;
using PersonnelManagement.Domain.PersonnelDomain;
using PersonnelManagement.Dtos;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;

namespace PersonnelManagement.Controllers
{
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class ClientController : ControllerBase
    {
        protected readonly IMediator mediatorp;

        public ClientController(IMediator mediatorp)
        {
            this.mediatorp = mediatorp;
        }
        /// <summary>
        /// 客户类型分类显示树形
        /// </summary>
        /// <param name="cd"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<ClientCategoryDto>>> ProductTypeSelect([FromQuery] ClientcategoryShowCommand command)
        {
            ApiResult<List<ClientCategoryDto>> result = new ApiResult<List<ClientCategoryDto>>();
            try
            {

                result.Code = 200;
                result.Message = "显示成功！";
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }




        /// <summary>
        /// 客户添加
        /// </summary>
        /// <param name="cd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> ClientCenter(ClientAddCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.AddDtos.Office_Id = sid;
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.AddDtos.AddName = name;
                command.AddDtos.AddTime=DateTime.Now;
                //获取用户ID
                var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                command.AddDtos.User_Id = int.Parse(userId);
                result.Code = 200;
                result.Message = "显示成功！";
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }


        /// <summary>
        /// 客户显示
        /// </summary>
        /// <param name="cd"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDto<ClientShowDto>>> ClientAllShow([FromQuery] ClientShowCommand command)
        {
            ApiResult<PageDto<ClientShowDto>> result = new ApiResult<PageDto<ClientShowDto>>();
            try
            {
                result.Code = 200;
                result.Message = "显示成功！";
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }




        /// <summary>
        /// 客户删除
        /// </summary>
        /// <param name="cd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> ClientDelete(ClientDelCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
            
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.DelName = name;
                result.Code = 200;
                result.Message = "删除成功！";
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }


        /// <summary>
        /// 客户编辑
        /// </summary>
        /// <param name="cd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> ClientUpd(ClientUpdCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {

                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.UpdDtos.UpdName = name;
                command.UpdDtos.UpdTime = DateTime.Now;
                result.Code = 200;
                result.Message = "修改成功！";
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }


        /// <summary>
        /// 客户反填
        /// </summary>
        /// <param name="cd"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<Client>>> ClientBlikShow([FromQuery] ClientBlikCommand command)
        {
            ApiResult<List<Client>> result = new ApiResult<List<Client>>();
            try
            {
                result.Code = 200;
                result.Message = "显示成功！";
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }


        /// <summary>
        /// 客户状态修改
        /// </summary>
        /// <param name="cd"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<int>> ClientStateShow([FromQuery] ClientStateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {

                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.CRecipients = name;
                command.CApplicant = name;
              result.Code = 200;
                result.Message = "显示成功！";
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }



        /// <summary>
        /// 省份
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<Province>>> ProvinceSelect([FromQuery] ProvinceCommand command)
        {
            ApiResult<List<Province>> result = new ApiResult<List<Province>>();
            try
            {
                result.Code = 200;
                result.Message = "省份显示成功！";
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 城市
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<City>>> CitySelect([FromQuery] CityCommand command)
        {
            ApiResult<List<City>> result = new ApiResult<List<City>>();
            try
            {
                result.Code = 200;
                result.Message = "省份显示成功！";
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 统计图表
        /// </summary>
        /// <param name="cd"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<statisticsClientDtos>>> ClientAllShowTubiao()
        {
            ApiResult<List<statisticsClientDtos>> result = new ApiResult<List<statisticsClientDtos>>();
            try
            {
                result.Code = 200;
                result.Message = "显示成功！";
                result.Data = await mediatorp.Send(new statisticsClientCommand());
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }




    }
}
